<template>
  <view class="">
    <button v-if="savedFilePath" @click="openPDF">查看 PDF</button>
    <view v-if="loading" style="padding: 12px 0; text-align: center">
      加载中...
    </view>
    <view style="padding: 12px 0; text-align: center">
      {{ error }}
    </view>
    <view style="padding: 12px 0; text-align: center">{{ errMsg }}</view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        tempFilePath: "",
        loading: false,
        error: "",
        errMsg: "",
        // #ifdef MP-WEIXIN
        ossHost: "https://qcmp-pro.oss-cn-beijing.aliyuncs.com",
        // #endif
        // #ifdef H5
        ossHost: "",
        // #endif
        ossPath: "",

        savedFilePath: "",
      };
    },
    onLoad(options) {
      if (options.name && options.name.toLowerCase().endsWith(".pdf")) {
        this.ossPath = options.name;
        this.fetchAndViewPdf(options.name);
      }
    },
    onShow() {},
    methods: {
      openPDF() {
        const that = this;
        uni.openDocument({
          filePath: that.savedFilePath,
          fileType: "pdf",
          success: () => {
            console.log("PDF 打开成功");
            that.error = "PDF 打开成功";
          },
          fail: (err) => {
            console.error("打开 PDF 失败:", err);
            that.error = "打开 PDF 失败，请重试";
          },
        });
      },
      saveFileWeixin(tempFilePath, callback) {
        const that = this;
        uni.saveFile({
          tempFilePath: tempFilePath,
          success: (savedRes) => {
            // debugger;
            console.log("文件已保存:", savedRes.savedFilePath);
            that.error = "加载完成";
            that.savedFilePath = savedRes.savedFilePath;
            callback();
          },
          fail: (err) => {
            // debugger;
            console.error("保存文件失败:", err);
            that.loading = false;
            that.error = "保存文件失败1";
          },
        });
      },
      // 获取地址并发送
      getLocationAndSend(paramObj, callback, error) {
        const that = this;
        uni.getLocation({
          type: "gcj02",
          success: function (res) {
            console.log("当前位置的经度：" + res.longitude);
            console.log("当前位置的纬度：" + res.latitude);
            paramObj.position = res.longitude + "," + res.latitude;
            that.sendLogData(paramObj, callback, error);
          },
          fail: function (res) {
            console.log(res);
            // 测试代码
            paramObj.position = "0,0";
            paramObj.bak1 = "no_position";
            that.sendLogData(paramObj, callback, error);
            // end
            if (error) {
              error(res);
            }
          },
        });
      },
      // 发送
      sendLogData(paramObj, callback, error) {
        const userInfoObj = uni.getStorageSync("userInfo");
        const body = {
          // createBy: "1",
          // position: paramObj.position,
          // reportPath: paramObj.reportPath,
          nickName: userInfoObj.nickName,
          wxNumber: null,
          ...paramObj,
        };
        debugger
        let baseUrl = "https://zlyz.jdsn.cn";
        // #ifdef H5
        baseUrl = "";
        // #endif
        uni.request({
          url: baseUrl + "/t/sysOperationLog/add",
          method: "post",
          header: {
            Accept: "application/json",
            // Authorization: "Bearer sk-ddaeab9c356b4c2e96fad54780fc67d7",
          },
          data: body,
          success: (res) => {
            if (callback) {
              callback(res.data);
            }
          },
          fail: (res) => {
            if (error) {
              error(res);
            }
          },
        });
      },
      // 获取文档
      fetchAndViewPdf(ossPath) {
        const that = this;
        this.loading = true;
        this.error = "";
        // debugger;
        try {
          uni.downloadFile({
            url: this.ossHost + ossPath,
            success: (res) => {
              // debugger;
              if (res.statusCode === 200) {
                // #ifdef MP-WEIXIN
                that.saveFileWeixin(res.tempFilePath, () => {
                  that.loading = false;
                  that.openPDF();
                });
                // #endif
                // #ifdef H5
                that.savedFilePath = res.tempFilePath;
                that.loading = false;
                that.openPDF();
                // #endif
              }
              that.tempFilePath = res.tempFilePath;
              that.getLocationAndSend({ reportPath: ossPath }, (data) => {
                console.log(data.message);
              });
            },
            fail: (err) => {
              // debugger;
              // console.error("下载 PDF 失败:", err);
              that.loading = false;
              that.error = "下载 PDF 失败";
              that.errMsg = err.errMsg;
            },
          });
        } catch (err) {
          that.loading = false;
          this.error = "加载 PDF 失败，请检查网络";
        } finally {
          // this.loading = false;
        }
      },
    },
  };
</script>

<style></style>
